import { AddUserModel, UserModel } from '@/api/system/user/UserModel'
import useInstance from '@/hooks/useInstance'
import { EditType, Title } from '@/types/BaseEnum'
import { DialogModel } from '@/types/BaseType'
import { ElForm } from 'element-plus'
import { ref } from 'vue'

export default function useUserAddAndEdit(
    dialogModel: DialogModel,
    onShow: any,
    onClose: any,
    addModel: UserModel,
    emit: any
) {
    const { global } = useInstance()

    //表单ref属性
    const addUserForm = ref<InstanceType<typeof ElForm>>()

    const onConfirm = () => {
        addUserForm.value?.validate((valid) => {
            if (valid) {
                //把表单数据返回给父组件
                emit('save', addModel)
                //关闭弹框
                onClose()
            }
        })
    }
    const show = (type: string, row?: AddUserModel) => {
        console.log('show：', row)
        // 设置弹框属性
        type == EditType.ADD
            ? (dialogModel.title = Title.ADD)
            : (dialogModel.title = Title.EDIT)

        // 显示弹框
        onShow()
        // 清空表单
        global.$resetForm(addUserForm.value, addModel)
        // 把要编辑的数据，放到表单绑定的model里面
        global.$objCoppy(row, addModel)

        addModel.type = type
    }

    return {
        onConfirm,
        show,
        addUserForm
    }
}
